package com.rongshu.view.penpal.present;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.penpal.bean.PenpalNoticeResponse;
import com.rongshu.view.penpal.view.PenpalNoticeView;
import com.widget.toast.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2018/11/26.
 * description：
 */
public class PenpalNoticePresenter extends BasePresenter<PenpalNoticeView> {

    public PenpalNoticePresenter(Context context) {
        super(context);
    }

    public void getNotice(int penpalwill_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("penpalwill_id", penpalwill_id);
        RetorfitService.getNotice(UserInfoUtils.getUserId(), penpalwill_id,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<PenpalNoticeResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(PenpalNoticeResponse object) {
                getView().callBackNotces(object);
            }
        });
    }

    public void setTopNotice(int penpalwill_id, int noticeId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("penpalwill_id", penpalwill_id);
        map.put("announCement_id", noticeId);
        RetorfitService.setTopPenpalNotice(UserInfoUtils.getUserId(), penpalwill_id, noticeId,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                ToastUtils.show(object.getErrMsg());
                if (object.getErrCode() == 0) {
                    getView().callBackAddNoticeResult();
                }
            }
        });
    }

    public void deleteNotice(int penpalwill_id, int noticeId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("penpalwill_id", penpalwill_id);
        map.put("announCement_id", noticeId);
        RetorfitService.deletePenpalNotice(UserInfoUtils.getUserId(), penpalwill_id, noticeId,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                ToastUtils.show(object.getErrMsg());
                if (object.getErrCode() == 0) {
                    getView().callBackDeleteNoticeResult();
                }
            }
        });
    }

}
